﻿using Inovout.Kuajing.Models;
using Inovout.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Inovout.Kuajing.Services
{
    public class PurchaseOrderService : RepositoryService<PurchaseOrder>
    {
        private WangCangStoreService wangCangStoreService;
        private CommodityService commodityService = ComponentRegistry.Resolve<CommodityService>();
        public PurchaseOrderService()
        {
            wangCangStoreService = new WangCangStoreService();
        }
        private object noObject = new object();
        public override void Create(PurchaseOrder entity)
        {
            lock (noObject)
            {
                Random ro = new Random();
                entity.No = "P" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ro.Next(10, 99).ToString();
            }
            foreach (var item in entity.Items)
            {
                item.PurchaseOrder = entity;
                item.Commodity = commodityService.GetByCode(item.CommodityCode);
            }
            base.Create(entity);
            if (!wangCangStoreService.CreatePurchaseOrder(entity))
            {
                throw new Exception("WangCang error.");
            }
        }

    }
}

